local yilve = fk.CreateSkill{
  name = "hanqing__yilve",
}

Fk:loadTranslationTable{
  ["hanqing__yilve"] = "移略",
  [":hanqing__yilve"] = "每回合限一次，你指定或成为基本牌或普通锦囊牌的唯一目标后，你可以将使用者或目标区域内一张牌移至对方的相同区域，"..
  "因此失去牌的角色可以令此牌无效或额外结算一次。",

  ["#hanqing__yilve1-invoke"] = "移略：你对 %dest 使用%arg，选择你或其，将区域内一张牌移至对方对应区域",
  ["#hanqing__yilve2-invoke"] = "移略：%dest 对你使用%arg，选择你或其，将区域内一张牌移至对方对应区域",
  ["#hanqing__yilve-move"] = "移略：将一张牌移至对方区域内",
  ["#hanqing__yilve-choice"] = "移略：你可以令此%arg无效或额外结算一次",
  ["hanqing__yilve1"] = "此牌无效",
  ["hanqing__yilve2"] = "此牌额外结算一次",
}

Fk:addPoxiMethod{
  name = "hanqing__yilve",
  prompt = function (data, extra_data)
    return extra_data.prompt
  end,
  card_filter = function (to_select, selected, data, extra_data)
    return #selected == 0
  end,
  feasible = function(selected, data)
    return #selected == 1
  end,
  default_choice = function(data)
    if not data then return {} end
    local cids = data[2][1]
    return cids
  end,
}

local spec = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yilve.name) and
      (event == fk.TargetSpecified and data.firstTarget or event == fk.TargetConfirmed) and
      data.from ~= data.to and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
      data:isOnlyTarget(data.to) and player:usedSkillTimes(yilve.name, Player.HistoryTurn) == 0 and
      (not data.from:isKongcheng() or not data.to:isKongcheng() or #player.room:canMoveCardInBoard(nil, {data.from, data.to}) > 0)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#hanqing__yilve1-invoke::"..data.to.id..":"..data.card:toLogString()
    if event == fk.TargetConfirmed then
      prompt = "#hanqing__yilve2-invoke::"..data.from.id..":"..data.card:toLogString()
    end
    local targets = {}
    local from = data.from
    local to = data.to
    if not from:isKongcheng() then
      table.insert(targets, data.from)
    end
    if not to:isKongcheng() then
      table.insert(targets, data.to)
    end
    if #targets < 2 then
      if table.find(from:getCardIds("ej"), function (id)
        return from:canMoveCardInBoardTo(to, id)
      end) then
        table.insert(targets, data.from)
      end
      if table.find(to:getCardIds("ej"), function (id)
        return to:canMoveCardInBoardTo(from, id)
      end) then
        table.insert(targets, data.to)
      end
    end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = yilve.name,
      prompt = prompt,
      cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self, {tos = {data.from, data.to}, extra_data = tos[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = event:getCostData(self).extra_data
    local to = from == data.from and data.to or data.from
    local card_data = {}
    local dat = {
      from = from.id,
      to = to.id,
      skillName = yilve.name,
      prompt = "#hanqing__yilve-move:"..from.id..":"..to.id,
    }
    local visible_data = {}
    if not from:isKongcheng() then
      table.insert(card_data, {"$Hand", from:getCardIds("h")})
      for _, id in ipairs(from:getCardIds("h")) do
        if not player:cardVisible(id) then
          visible_data[tostring(id)] = false
        end
      end
    end
    local cards = table.filter(from:getCardIds("e"), function (id)
      return from:canMoveCardInBoardTo(to, id)
    end)
    if #cards > 0 then
      table.insert(card_data, {"$Equip", cards})
    end
    if #from:getCardIds("j") > 0 then
      table.insert(card_data, {"$Judge", from:getCardIds("j")})
      for _, id in ipairs(from:getCardIds("j")) do
        if not player:cardVisible(id) then
          visible_data[tostring(id)] = false
        end
      end
    end
    if next(visible_data) == nil then visible_data = nil end
    dat.visible_data = visible_data
    local card = room:askToPoxi(player, {
      poxi_type = yilve.name,
      data = card_data,
      extra_data = dat,
      cancelable = false,
    })
    room:moveCardTo(card, room:getCardArea(card[1]), to, fk.ReasonJustMove, yilve.name, nil, false, player)
    if from.dead then return end
    local choice = room:askToChoice(from, {
      choices = {"hanqing__yilve1", "hanqing__yilve2", "Cancel"},
      skill_name = yilve.name,
      prompt = "#hanqing__yilve-choice:::"..data.card:toLogString(),
    })
    if choice == "hanqing__yilve1" then
      data.use.nullifiedTargets = table.simpleClone(room.players)
    elseif choice == "hanqing__yilve2" then
      data.use.additionalEffect = (data.use.additionalEffect or 0) + 1
    end
  end,
}

yilve:addEffect(fk.TargetSpecified, spec)
yilve:addEffect(fk.TargetConfirmed, spec)

return yilve
